import cTitle from "components/title";
import { Toast, Field, Picker, Popup } from 'vant';
import { scrollMixin } from "utils/mixin";
// import { Field, CellGroup } from 'vant';
export default {
    mixins: [scrollMixin], //加载更多
    data() {
        return {
            showPicker: false,
            store_id: '', //店铺id
            objectMultiArray: [{
                text: '按重',
                value: 0
            }, {
                text: '按件',
                value: 1
            }],
            regional: "请选择计费方式",
            showCoverLayer: !1,
            calculate_type: '', //计费方式 0按重 1按件
            dispatch_name: '', //模板名称
            first_weight: '', //首重克数
            another_weight: '', //续重克数
            first_weight_price: '', //首重价格
            another_weight_price: '', //续重价格
            first_piece: '', //首件个数
            another_piece: '', //续件个数
            first_piece_price: '', //首件价格
            another_piece_price: '', //续件价格
            template_id: '', //模板id
            CTitle: '',//标题
        };
    },
    activated() {
        console.log(this.$route.params, 'lllll')
        this.store_id = this.$route.params.store_id
        this.type = this.$route.params.type

        if (this.type == 1) {
            this.CTitle = '模板编辑'
            this.template_id = this.$route.params.template_id
            this.getMubanDetail()
        } else {
            this.CTitle = '模板发布'
            this.initData()
        }
    },
    methods: {
        initData() {
            this.regional = "请选择计费方式";
            this.showCoverLayer = !1;
            this.calculate_type = ''; //计费方式 0按重 1按件
            this.dispatch_name = ''; //模板名称
            this.first_weight = ''; //首重克数
            this.another_weight = ''; //续重克数
            this.first_weight_price = ''; //首重价格
            this.another_weight_price = ''; //续重价格
            this.first_piece = ''; //首件个数
            this.another_piece = ''; //续件个数
            this.first_piece_price = ''; //首件价格
            this.another_piece_price = ''; //续件价格
            this.template_id = ''; //模板id
        },
        openPicker() {
            this.showPicker = true
        },
        onCancel() {
            this.showPicker = false
        },
        onConfirm(e) {
            console.log(e)
            this.showPicker = false
            this.regional = e.text
            let index = e.value
            console.log(index)
            this.calculate_type = index
            // let index = e.detail.value
            // this.setData({
            //   calculate_type: index,
            //   regional: this.data.objectMultiArray[index].name
            // })
        },
        getList() {
            let json = { store_id: this.store_id };
            $http
                .post(
                    "HuanNiu.list_ps_muban",
                    json,
                    "获取中"
                )
                .then(
                    response => {
                        if (response.result == 1) {
                            // this.commodity_data = response.data
                            // console.log(this.commodity_data,'this.commodity_data')
                            //   Toast(response.msg);
                            //   this.$router.go(-1);
                        } else {
                            Toast(response.msg);
                        }
                    });
        },
        getMubanDetail() {
            console.log(this.template_id,'this.template_id')
            let json = { id: this.template_id };
            $http
                .post(
                    "HuanNiu.get_ps_muban",
                    json,
                    "获取中"
                )
                .then(
                    response => {
                        if (response.result == 1) {
                            console.log(response)
                            this.calculate_type = response.data.calculate_type; //计费方式 0按重 1按件
                            this.dispatch_name = response.data.dispatch_name; //模板名称
                            this.first_weight = response.data.first_weight; //首重克数
                            this.another_weight = response.data.another_weight; //续重克数
                            this.first_weight_price = response.data.first_weight_price; //首重价格
                            this.another_weight_price = response.data.another_weight_price; //续重价格
                            this.first_piece = response.data.first_piece; //首件个数
                            this.another_piece = response.data.another_piece; //续件个数
                            this.first_piece_price = response.data.first_piece_price; //首件价格
                            this.another_piece_price = response.data.another_piece_price; //续件价格
                            this.regional = response.data.calculate_type == 0 ? '按重' : '按件'
                        } else {
                            Toast(response.msg);
                        }
                    });
        },
        publishEvent() {
            if (this.dispatch_name == '') {
                Toast("请填写模板名称");
                return
            }
            if (this.regional == '请选择计费方式') {
                Toast("请选择计费方式");
                return
            }
            if (this.calculate_type == 0) {
                if (this.first_weight == '') {
                    Toast("请填写首重克数");
                    return
                }
                if (this.another_weight == '') {
                    Toast("请填写续重克数");
                    return
                }
                if (this.first_weight_price == '') {
                    Toast("请填写首重价格");
                    return
                }
                if (this.another_weight_price == '') {
                    Toast("请填写续重价格");
                    return
                }
            } else if (this.calculate_type == 1) {
                if (this.first_piece == '') {
                    Toast("请填写首件个数");
                    return
                }
                if (this.another_piece == '') {
                    Toast("请填写续件个数");
                    return
                }
                if (this.first_piece_price == '') {
                    Toast("请填写首件价格");
                    return
                }
                if (this.another_piece_price == '') {
                    Toast("请填写续件价格");
                    return
                }
            }
            if (this.type == 1) {
                this.editTemplate()
            } else {
                this.addTemplate()
            }
        },
        addTemplate() {
            this.showCoverLayer = true
            console.log('addTemplate')
            let _this = this;
            let json = {
                store_id: _this.store_id,
                dispatch_name: _this.dispatch_name,
                calculate_type: _this.calculate_type,
                first_weight: _this.first_weight,
                another_weight: _this.another_weight,
                first_weight_price: _this.first_weight_price,
                another_weight_price: _this.another_weight_price,
                first_piece: _this.first_piece,
                another_piece: _this.another_piece,
                first_piece_price: _this.first_piece_price,
                another_piece_price: _this.another_piece_price
            };
            $http
                .post(
                    "HuanNiu.add_ps_muban",
                    json,
                    "发布中"
                )
                .then(
                    response => {
                        if (response.result == 1) {
                            Toast('上传成功');
                            this.$router.go(-1);
                            // this.commodity_data = response.data
                            // console.log(this.commodity_data,'this.commodity_data')
                            //   Toast(response.msg);
                            //   this.$router.go(-1);
                        } else {
                            Toast(response.msg);
                            this.showCoverLayer = false
                        }
                    });
        },
        editTemplate() {
            console.log('编辑')
            this.showCoverLayer = true
            console.log('addTemplate')
            let _this = this;
            let json = {
                id: _this.template_id,
                // store_id: _this.store_id,
                dispatch_name: _this.dispatch_name,
                calculate_type: _this.calculate_type,
                first_weight: _this.first_weight,
                another_weight: _this.another_weight,
                first_weight_price: _this.first_weight_price,
                another_weight_price: _this.another_weight_price,
                first_piece: _this.first_piece,
                another_piece: _this.another_piece,
                first_piece_price: _this.first_piece_price,
                another_piece_price: _this.another_piece_price
            };
            $http
                .post(
                    "HuanNiu.edit_ps_muban",
                    json,
                    " "
                )
                .then(
                    response => {
                        if (response.result == 1) {
                            Toast('编辑成功');
                            this.$router.go(-1);
                            // this.commodity_data = response.data
                            // console.log(this.commodity_data,'this.commodity_data')
                            //   Toast(response.msg);
                            //   this.$router.go(-1);
                        } else {
                            Toast(response.msg);
                            this.showCoverLayer = false
                        }
                    });
        },
        
        // add(){
        //   let json={store_id:this.store_id,choose_goods:this.result};
        //   $http
        //     .post(
        //       "plugin.nearby-store-goods.frontend.controllers.goods.index",
        //       json,
        //       "获取中"
        //     )
        //     .then(
        //       response => {
        //         if (response.result == 1) {
        //           Toast(response.msg);
        //           this.$router.go(-1);
        //         } else {
        //           Toast(response.msg);
        //         }
        //       });
        // },

        getNetData() {
            let that = this;
            let json = { store_id: this.store_id, page: this.page };
            $http
                .post(
                    "plugin.store-cashier.frontend.store.near-by-goods.index",
                    json,
                    "获取中"
                )
                .then(
                    response => {
                        if (response.result == 1) {
                            that.arr = response.data.category;
                            that.datas = response.data.goods_data.data;
                            that.total_page = response.data.goods_data.last_page;
                            if (!that.total_page) {
                                that.total_page = 0;
                            }
                            that.arr.forEach((item, index, key) => {
                                let json = {};
                                json.text = item.name;
                                json.value = item.id;
                                that.option1.push(json);
                            });
                        } else {
                            // 判断权限
                            if (response.data.url) {
                                that.$notify(response.msg);
                                window.location.href = response.data.url;
                                return 0;
                            } else {
                                // that.$message.error(response.msg);
                                // that.$message.error("暂无商品分类,请添加分类");
                                that.$notify("暂无商品分类,请添加分类");
                            }
                        }
                    },
                    function (response) {
                        console.log(response);
                    }
                );
        },

    },
    components: {
        cTitle
    }
};
